<?php
class Model_User2 extends Model_Base implements Zend_Acl_Role_Interface {
   protected $_pk = 'id';
   protected $_fields = array(
      'id', 'username', 'password', 'firstname', 'lastname', 'email', 'address', 'zip', 'city', 'country'
   );
   protected $_table = 'users';
   protected $_name = 'users';
   
   public function getFields() {
      return $this->_fields;
   }
   
   public function getId() {
      return $this->id;
   }
   
   public function setId($id) {
      $this->id = $id;
   }
   
   public function getUserName() {
      return $this->username;
   }
   
   public function setUserName($username) {
      $this->username = $username;
   }
   
   /**
	 * @return string
	 */
   public function getRoleId() {
      return 'user-' . $this->getId();
   }
   
   public function getGroups1() {
      $db = Zend_Registry::get('db');
      $sql = sprintf("
         SELECT 
            g.id, g.name 
         FROM 
            user_groups ug
            left join users u on (ug.user_id = u.id)
            left join groups g on (ug.group_id = g.id)
         WHERE 
            u.id = %s
      ", $this->getId());
      //$row = $db->query($sql);
      $rows = $db->fetchRow($sql, array($this->getId()), Zend_Db::FETCH_ASSOC);
      $groups = array();
      foreach($rows as $row) {
         $group = new Model_Group();
         $group->setId($row['id']);
         $group->setName($row['username']);
         $groups[] = $group;
      }
      return $groups;
   }
   
   public function getGroups() {

      $db = Zend_Registry::get('db');
      $sql = '
         SELECT 
            g.id, g.name
         FROM 
            groups g
            LEFT JOIN user_groups ug ON (ug.group_id = g.id)
         WHERE
            ug.user_id = ?
         GROUP BY
            g.id
      ';
      $sql1 = 'SELECT id, username
                        FROM groups
                        WHERE id = ?';
      /*$sql = sprintf("
         SELECT 
            g.id, g.name 
         FROM 
            user_groups ug
            left join users u on (ug.user_id = u.id)
            left join groups g on (ug.group_id = g.id)
         WHERE 
            u.id = %s
      ", $this->getId());*/
      $groups = array();
      //Zend_Db_Adapter_Pdo_Mysql::fetchRow()
      $rows = $db->fetchAll($sql, array($this->getId()), Zend_Db::FETCH_ASSOC);
      foreach($rows as $row) {
         $group = new Model_Group();
         $group->setId($row['id']);
         $group->setName($row['name']);
         $groups[] = $group;
      }
      return $groups;
   }
   
}